home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / pyshared / softwareproperties / gtk / DialogAddSourcesList.py < prev    next >
Encoding:
Python Source  |  2009-03-27  |  4.8 KB  |  126 lines

  1. #!/usr/bin/env python
  2. import pygtk
  3. import gtk
  4. import gtk.glade
  5. import gobject
  6. import os
  7. from optparse import OptionParser
  8. from gettext import gettext as _
  9. import gettext
  10. import urllib
  11.  
  12. from aptsources.sourceslist import SourcesList, SourceEntryMatcher
  13.  
  14. class DialogAddSourcesList:
  15.     def __init__(self, parent, sourceslist, source_renderer,
  16.                  get_comparable, datadir, file):
  17.         print file
  18.         self.parent = parent
  19.         self.source_renderer = source_renderer
  20.         self.sourceslist = sourceslist
  21.         self.get_comparable = get_comparable
  22.         self.file = self.format_uri(file)
  23.         self.glade = gtk.glade.XML(os.path.join(datadir,
  24.                      "glade/dialogs.glade"))
  25.         self.glade.signal_autoconnect(self)
  26.         self.dialog = self.glade.get_widget("dialog_add_sources_list")
  27.         self.label = self.glade.get_widget("label_sources")
  28.         self.button_add = self.glade.get_widget("button_add")
  29.         self.button_cancel = self.glade.get_widget("button_cancel")
  30.         self.button_replace = self.glade.get_widget("button_replace")
  31.         self.treeview = self.glade.get_widget("treeview_sources")
  32.         self.scrolled = self.glade.get_widget("scrolled_window")
  33.         self.image = self.glade.get_widget("image_sources_list")
  34.  
  35.         self.dialog.realize()
  36.         if self.parent != None:
  37.             self.dialog.set_transient_for(parent)
  38.         else:
  39.             self.dialog.set_title(_("Add Software Channels"))
  40.         self.dialog.window.set_functions(gtk.gdk.FUNC_MOVE)
  41.  
  42.         # Setup the treeview
  43.         self.store = gtk.ListStore(gobject.TYPE_STRING)
  44.         self.treeview.set_model(self.store)
  45.         cell = gtk.CellRendererText()
  46.         cell.set_property("xpad", 2)
  47.         cell.set_property("ypad", 2)
  48.         column = gtk.TreeViewColumn("Software Channel", cell, markup=0)
  49.         column.set_max_width(500)
  50.         self.treeview.append_column(column)
  51.  
  52.         # Parse the source.list file
  53.         try:
  54.             self.new_sources = SingleSourcesList(self.file)
  55.         except:
  56.             self.error()
  57.             return
  58.  
  59.         # show the found channels or an error message
  60.         if len(self.new_sources.list) > 0:
  61.             counter = 0
  62.  
  63.             for source in self.new_sources.list:
  64.                 if source.invalid or source.disabled:
  65.                     continue
  66.                 self.new_sources.matcher.match(source)
  67.             # sort the list
  68.             self.new_sources.list.sort(key=self.get_comparable)
  69.             
  70.             for source in self.new_sources.list:
  71.                 if source.invalid or source.disabled:
  72.                     continue
  73.                 counter = counter +1
  74.                 line = self.source_renderer(source)
  75.                 self.store.append([line])
  76.             if counter == 0:
  77.                 self.error()
  78.                 return
  79.  
  80.             header = gettext.ngettext("Install software additionally or "
  81.                                       "only from this source?",
  82.                                       "Install software additionally or "
  83.                                       "only from these sources?",
  84.                                       counter)
  85.             body = _("You can either add the following sources or replace your "
  86.                      "current sources by them. Only install software from "
  87.                      "trusted sources.")
  88.             self.label.set_markup("<big><b>%s</b></big>\n\n%s" % (header, body))
  89.         else:
  90.             self.error()
  91.             return
  92.  
  93.     def error(self):
  94.         self.button_add.hide()
  95.         self.button_cancel.set_use_stock(True)
  96.         self.button_cancel.set_label("gtk-close")
  97.         self.button_replace.hide()
  98.         self.scrolled.hide()
  99.         self.image.set_from_stock(gtk.STOCK_DIALOG_ERROR, gtk.ICON_SIZE_DIALOG)
  100.         header = _("There are no sources to install software from")
  101.         body = _("The file '%s' does not contain any valid "
  102.                  "software sources." % self.file)
  103.         self.label.set_markup("<big><b>%s</b></big>\n\n%s" % (header, body))
  104.  
  105.     def run(self):
  106.         res = self.dialog.run()
  107.         self.dialog.destroy()
  108.         return res, self.new_sources
  109.  
  110.     def format_uri(self, uri):
  111.         path = urllib.url2pathname(uri) # escape special chars
  112.         path = path.strip('\r\n\x00') # remove \r\n and NULL
  113.         if path.startswith('file:\\\\\\'): # windows
  114.             path = path[8:] # 8 is len('file:///')
  115.         elif path.startswith('file://'): #nautilus, rox
  116.             path = path[7:] # 7 is len('file://')
  117.         elif path.startswith('file:'): # xffm
  118.             path = path[5:] # 5 is len('file:')
  119.         return path
  120.  
  121. class SingleSourcesList(SourcesList):
  122.     def __init__(self, file):
  123.         self.matcher = SourceEntryMatcher("/usr/share/update-manager/channels/")
  124.         self.list = []
  125.         self.load(file)
  126.